<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: Circle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\note;

use app\api\model\dao\Note;
use app\api\model\dao\NoteAccusation;
use app\library\Error;
use app\library\Flag;
use app\library\PageBase;
use think\Exception;
use think\Validate;

class ChargeNote extends PageBase
{

    private $objNote;
    private $objNoteAccusation;

    public function __construct()
    {
        parent::__construct();

        $this->objNote = new Note();
        $this->objNoteAccusation = new NoteAccusation();
    }

    /**
     * @param $param
     * @return array|bool
     * @DESC 验证接口字段
     */
    public function checkParam($param)
    {
        $rule = [
            'note_id' => 'require',
        ];
        $msg = [
            'note_id.require' => '请选择要举报的帖子',
        ];

        $validate = new Validate($rule, $msg);
        $result = $validate->check($param);
        if (!$result) {
            return $validate->getError();
        }
        return '';
    }

    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        // 1、参数校验
        $ret = $this->checkParam($param);
        if ($ret) {
            return $this->getRes(Error::ERR_PARAM, $ret);
        }

        $note_info = $this->objNote->getNoteByid($param['note_id']);
        if (false === $note_info) {
            return $this->getRes(Error::ERR_SYS, Error::$arr_err_msg[Error::ERR_SYS] . 'get note info from db fail');
        }

        if (empty($note_info)) {
            return $this->getRes(Error::ERR_NOTE_NOT_EXIST);
        }

        // 3、校验是否被删除
        if (Flag::CONST_NOTE_DELETED == $note_info['is_delete']) {
            return $this->getRes(Error::ERR_NOTE_DELETED);
        }

        // 5、构造参数
        $input = [
            'note_id' => $note_info['id'],
            'note_userid' => $note_info['creator'],
            'userid' => $this->user_id,
            'create_time' => time(),
        ];

        $insertID = $this->objNoteAccusation->add($input);
        if (false === $insertID) {
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();
    }
}
